package com.winchain.chainbill.bill.base.service.impl;

import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.winchain.chainbill.bill.base.dao.CbHldrDao;
import com.winchain.chainbill.bill.base.po.CbHldr;
import com.winchain.chainbill.bill.base.service.CbHldrService;
import com.winchain.chainbill.bill.biz.query.vo.BillNode;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("cbHldrService")
public class CbHldrServiceImpl extends PersistentServiceImpl<CbHldr> implements CbHldrService {
	@Autowired
	private CbHldrDao cbHldrDao;

	public List<CbHldr> getHolders(String billId, int hldrLvl) {
		Example ex = new Example(CbHldr.class);
		ex.setOrderByClause("hldr_lvl");
		Example.Criteria criteria = ex.createCriteria();
		criteria.andEqualTo("billId", billId);
		criteria.andLessThanOrEqualTo("hldrLvl", Integer.valueOf(hldrLvl));

		return this.cbHldrDao.selectByExample(ex);
	}

	public List<BillNode> getBillNodesByRootId(String rootId) {
		return this.cbHldrDao.getBillNodesByRootId(rootId);
	}

	public List<BillNode> getBillNodes(String billId, int hldrLvl) {
		return this.cbHldrDao.getBillNodes(billId, hldrLvl);
	}

	public List<BillNode> getBillNodesRmn(String rootId, String prntId, String billId, int hldrLvl) {
		return this.cbHldrDao.getBillNodesRmn(rootId, prntId, billId, hldrLvl);
	}

	public List<BillNode> getBillNodesSplit(String rootId, String prntId, String billId, int hldrLvl) {
		List<BillNode> node = this.cbHldrDao.getBillNodesSplit(rootId, prntId, billId, hldrLvl);
		List<BillNode> nodes = new ArrayList();
		if (node.size() > 3) {
			for (BillNode row : node) {
				int level = hldrLvl - 1;
				if ((row.getHldrLvl().intValue() != level) || (!row.getBillId().equals(prntId))) {
					nodes.add(row);
				}
			}
		} else {
			return node;
		}
		return nodes;
	}
}
